package com.fulihui.usercore.service;

import com.fulihui.usercore.api.UserVcodeService;
import com.fulihui.usercore.config.RedisUtil;
import com.fulihui.usercore.core.processor.vcode.VcodeProcessor;
import com.fulihui.usercore.core.processor.vcode.VcodeProcessorRegistor;
import com.fulihui.usercore.enums.VcodeProcessTypeEnum;
import com.fulihui.usercore.request.vcode.VcodeCheckRequest;
import com.fulihui.usercore.request.vcode.VcodeSendRequest;
import org.apache.dubbo.config.annotation.Service;
import org.near.servicesupport.error.CommonServiceErrorEnum;
import org.near.servicesupport.error.InvokeServiceException;
import org.near.servicesupport.result.BaseResult;
import org.near.servicesupport.result.ResultBuilder;
import org.near.servicesupport.result.TSingleResult;
import org.near.servicesupport.util.ServiceAssert;
import org.near.toolkit.common.EnumUtil;
import org.near.toolkit.common.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;


import static org.near.servicesupport.error.Errors.Commons.REQUEST_PARAMETER_ERROR;

/**
 * Created by Willard.Hu on 2016/5/17.
 */
@Service(version = "1.0.0")
public class UserVcodeServiceImpl implements UserVcodeService {
    private final transient Logger LOG = LoggerFactory.getLogger(getClass());
    @Autowired
    private VcodeProcessorRegistor vcodeProcessorRegistor;
    @Autowired
    RedisUtil redisUtil;
    private static final String SESSION_KEY = "SESSION_CAPTCHA";

    @Override
    public BaseResult informationSend(VcodeSendRequest request) {
        LOG.info("发送短信请求request：{}", request);
        //fixme 先验证图形验证码是否正确，如果不正确 不发送短信
        ServiceAssert.notNull(request, REQUEST_PARAMETER_ERROR);
        Assert.hasLength(request.getImageCode(), "图形验证码不能为空！");
        String key = SESSION_KEY + "_" + request.getVcodeNamespace() + "_" + request.getAccountNo();
        String imageCodeRedis = (String) redisUtil.get(key);
        if (!StringUtil.equals(StringUtil.toLowerCase(imageCodeRedis),
                StringUtil.toLowerCase(request.getImageCode()))) {
            return ResultBuilder.fail(301, "图形验证码错误，请重新输入！");
        }


        VcodeProcessTypeEnum processType = EnumUtil.queryByCode(request.getProcessType(),
                VcodeProcessTypeEnum.class);
        VcodeProcessor vcodeProcessor = null;
        if (processType != null) {
            vcodeProcessor = vcodeProcessorRegistor.getProcessor(processType);
        }
        if (vcodeProcessor == null) {
            LOG.error("处理类型{}未能找到处理类实例", request.getProcessType());
            throw new InvokeServiceException(CommonServiceErrorEnum.REQUEST_PARAMETER_ERROR);
        }
        vcodeProcessor.send(request);
        return ResultBuilder.succ(BaseResult.class);
    }

    @Override
    public BaseResult send(VcodeSendRequest request) {
        LOG.info("发送短信请求request：{}", request);
        VcodeProcessTypeEnum processType = EnumUtil.queryByCode(request.getProcessType(),
                VcodeProcessTypeEnum.class);
        VcodeProcessor vcodeProcessor = null;
        if (processType != null) {
            vcodeProcessor = vcodeProcessorRegistor.getProcessor(processType);
        }
        if (vcodeProcessor == null) {
            LOG.error("处理类型{}未能找到处理类实例", request.getProcessType());
            throw new InvokeServiceException(CommonServiceErrorEnum.REQUEST_PARAMETER_ERROR);
        }
        vcodeProcessor.send(request);
        return ResultBuilder.succ(BaseResult.class);
    }

    @Override
    public TSingleResult<Boolean> check(VcodeCheckRequest request) {
        VcodeProcessTypeEnum processType = EnumUtil.queryByCode(request.getProcessType(),
                VcodeProcessTypeEnum.class);
        VcodeProcessor vcodeProcessor = null;
        if (processType != null) {
            vcodeProcessor = vcodeProcessorRegistor.getProcessor(processType);
        }
        if (vcodeProcessor == null) {
            LOG.error("处理类型{}未能找到处理类实例", request.getProcessType());
            throw new InvokeServiceException(CommonServiceErrorEnum.REQUEST_PARAMETER_ERROR);
        }
        return ResultBuilder.succTSingle(vcodeProcessor.check(request));
    }

    @Override
    public TSingleResult<Boolean> checkNotDeleteKey(VcodeCheckRequest request) {
        VcodeProcessTypeEnum processType = EnumUtil.queryByCode(request.getProcessType(),
                VcodeProcessTypeEnum.class);
        VcodeProcessor vcodeProcessor = null;
        if (processType != null) {
            vcodeProcessor = vcodeProcessorRegistor.getProcessor(processType);
        }
        if (vcodeProcessor == null) {
            LOG.error("处理类型{}未能找到处理类实例", request.getProcessType());
            throw new InvokeServiceException(CommonServiceErrorEnum.REQUEST_PARAMETER_ERROR);
        }
        return ResultBuilder.succTSingle(vcodeProcessor.checkNotDeleteKey(request));
    }

}
